package com.wsd.jing.controller.system;

import com.wsd.jing.annotation.ControllerLog;
import com.wsd.jing.entity.Menu;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.request.CommonResponse;
import com.wsd.jing.service.IMenuService;
import com.wsd.jing.validator.group.InsertGroup;
import com.wsd.jing.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.controller
 * @className: MenuController
 * @description: 菜单前端控制器
 * @createDate 2022-11-29 04:06
 * @copyright 2022-11-29 04:06 www.tom.com Inc. All rights reserved.
 */
@Api(tags = "菜单")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rest/menu")
public class MenuController {

    private final IMenuService menuService;

    @ApiOperation(value = "新增")
    @PostMapping()
    @ControllerLog(action = "新增菜单", message = "新增菜单失败")
    // @SaCheckPermission("menu:add")
    public CommonResponse insertMenu(@Validated({InsertGroup.class}) @RequestBody Menu menu) {
        return CommonResponse.success(menuService.insertMenu(menu), "新增成功");
    }

    @ApiOperation(value = "修改")
    @PutMapping()
    @ControllerLog(action = "修改菜单", message = "修改菜单失败")
    // @SaCheckPermission("menu:update")
    public CommonResponse updateMenu(@Validated(UpdateGroup.class) @RequestBody Menu menu) {
        return CommonResponse.success(menuService.updateMenu(menu), "修改成功");
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "菜单id", paramType = "path", dataType = "Integer", required = true)
    @DeleteMapping("/{id}")
    @ControllerLog(action = "删除菜单", message = "删除菜单失败")
    // @SaCheckPermission("menu:delete")
    public CommonResponse deleteMenu(@PathVariable("id") Integer id) {
        return CommonResponse.success(menuService.deleteMenu(id), "删除成功");
    }

    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "菜单id逗号分割字符", paramType = "query", dataType = "String", required = true)
    @DeleteMapping("")
    @ControllerLog(action = "批量删除菜单", message = "批量删除菜单失败")
    // @SaCheckPermission("menu:batch:delete")
    public CommonResponse deleteMenus(@RequestParam("ids") String ids) {
        return CommonResponse.success(menuService.deleteMenus(ids), "批量删除成功");
    }

    @ApiOperation(value = "查询")
    @ApiImplicitParam(name = "id", value = "菜单id", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/{id}")
    @ControllerLog(action = "查询菜单", message = "查询菜单失败")
    // @SaCheckPermission("menu:get")
    public CommonResponse getMenu(@PathVariable("id") Integer id) {
        return CommonResponse.success(menuService.getMenu(id), "查询成功");
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    @ControllerLog(action = "查询菜单列表", message = "查询菜单列表失败")
    // @SaCheckPermission("menu:list")
    public CommonResponse listMenus(CommonRequest commonRequest, Menu menu) {
        return CommonResponse.success(menuService.listMenus(commonRequest, menu), "查询成功");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pages")
    @ControllerLog(action = "分页查询菜单", message = "分页查询菜单失败")
    // @SaCheckPermission("menu:page")
    public CommonResponse listPageMenus(CommonRequest commonRequest, Menu menu) {
        return CommonResponse.success(menuService.listPageMenus(commonRequest, menu), "查询成功");
    }

    @ApiOperation(value = "查询菜单/权限树")
    @ApiImplicitParam(name = "type", value = "type", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/tree/{type}")
    @ControllerLog(action = "查询菜单/权限树", message = "查询菜单/权限树失败")
    // @SaCheckPermission("menu:tree")
    public CommonResponse listTreeMenus(@PathVariable("type") Integer type) {
        if (type == 0) {
            return CommonResponse.success(menuService.listTreeMenus(), "查询成功");
        }
        return CommonResponse.success(menuService.listTreeAuths(), "查询成功");
    }
}
